﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VideoReflector.Models;

namespace VideoReflector.Controllers
{
    /// <summary>
    /// Controller tasked with updating session details during session review
    /// </summary>
    public class EditSessionDetailsController : TaskController
    {
        #region Private fields

        private string _therapistName;
        private string _groupName;
        private string _description;
        private Session _session;

        #endregion // Private fields

        #region Constructor

        /// <summary>
        /// Constructor for EditSessionDetailsController object
        /// </summary>
        /// <param name="session"> Session object to be edited </param>
        public EditSessionDetailsController(Session session)
        {
            _session = session;
            TherapistName = session.TherapistName;
            GroupName = session.GroupName;
            Description = session.Description;
        }

        #endregion // Constructor

        #region Public properties

        /// <summary>
        /// Name of therapist associated with session
        /// </summary>
        public string TherapistName
        {
            get
            {
                return _therapistName;
            }
            set
            {
                _therapistName = value;
                NotifyPropertyChanged("TherapistName");
            }
        }

        /// <summary>
        /// Name of group associated with session
        /// </summary>
        public string GroupName
        {
            get
            {
                return _groupName;
            }
            set
            {
                _groupName = value;
                NotifyPropertyChanged("GroupName");
            }
        }
        
        /// <summary>
        /// Description of session
        /// </summary>
        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
                NotifyPropertyChanged("Description");
            }
        }
                
        #endregion // Public properties

        #region Commands

        #region Ok

        private RelayCommand _okCommand;

        /// <summary>
        /// Finish editing session details and write updated values to session object
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new RelayCommand
                    (
                        param => this.Ok(),
                        param => this.CanOk()
                    );
                }
                return _okCommand;
            }
        }

        private void Ok()
        {
            _session.TherapistName = TherapistName;
            _session.GroupName = GroupName;
            _session.Description = Description;
            OnFinished(new TaskFinishedEventArgs(false));
        }

        private bool CanOk()
        {
            return TherapistName != _session.TherapistName || GroupName != _session.GroupName || Description != _session.Description;
        }

        #endregion // Ok  
        
        #region Cancel

        private RelayCommand _cancelCommand;

        /// <summary>
        /// Finish editing session details and discard updated values
        /// </summary>
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand
                    (
                        param => this.Cancel(),
                        param => this.CanCancel()
                    );
                }
                return _cancelCommand;
            }
        }

        private void Cancel()
        {
            OnFinished(new TaskFinishedEventArgs(true));
        }

        private bool CanCancel()
        {
            return true;
        }

        #endregion // Cancel  
                
        #endregion // Commands
    }
}
